package com.example.application.weather;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.application.R;
import com.example.application.task.WeatherTask1;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class FirstFragment extends Fragment{
    String TAG="aaa";
    TextView tv;
    View currentFragment;
    TextView tw_date,tw_temp,tw_con,tw_wind,tw_aircon,tw_city;
    TextView a2,a3,a4,a5,a6,a7;
    Handler handler;
    private ProgressBar bar;
    List<String> list;
    List<String> list2;
    RelativeLayout r ;
    public FirstFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Log.i(TAG, "onCreate: ...");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        currentFragment = inflater.inflate(R.layout.main_city_weather, container, false);
        return currentFragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //bar=getView().findViewById(R.id.progressBar2);
        r=getView().findViewById(R.id.rel);
        a2=getView().findViewById(R.id.a2);
        a3=getView().findViewById(R.id.a3);
        a4=getView().findViewById(R.id.a4);
        a5=getView().findViewById(R.id.a5);
        a6=getView().findViewById(R.id.a6);
        a7=getView().findViewById(R.id.a7);
        String curDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
        tw_date = getView().findViewById(R.id.frag_tv_date);
        tw_city = getView().findViewById(R.id.frag_tv_city);
        tw_temp = getView().findViewById(R.id.frag_tv_currenttemp);
        tw_con = getView().findViewById(R.id.frag_tv_condition);
        tw_wind = getView().findViewById(R.id.frag_tv_wind);
        tw_aircon = getView().findViewById(R.id.frag_tv_aircon);
        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==6){
                    list2 =(List<String>) msg.obj;
                    for(int j=0;j<7;j++){
                        a2.setText(a2.getText()+list2.get(j)+"\n");
                    }
                    for(int j=0;j<7;j++){
                        a3.setText(a3.getText()+list2.get(j+7)+"\n");
                    }
                    for(int j=0;j<7;j++){
                        a4.setText(a4.getText()+list2.get(j+14)+"\n");
                    }
                    for(int j=0;j<7;j++){
                        a5.setText(a5.getText()+list2.get(j+21)+"\n");
                    }
                    for(int j=0;j<7;j++){
                        a6.setText(a6.getText()+list2.get(j+28)+"\n");
                    }
                    for(int j=0;j<7;j++){
                        a7.setText(a7.getText()+list2.get(j+35)+"\n");
                    }
                    Log.i(TAG, "runlist2"+list2);
                }
                if (msg.what == 5) {
                    list = (List<String>) msg.obj;
                    tw_temp.setText(list.get(1));
                    tw_con.setText(list.get(0));
                    tw_wind.setText(list.get(2));
                    tw_aircon.setText("空气质量："+list.get(3));
                    tw_date.setText(curDateStr);
                    tw_city.setText("成都");
                }
                super.handleMessage(msg);
            }
        };

        WeatherTask1 mytask=new WeatherTask1();
        mytask.setHandler(handler);
        Thread t = new Thread(mytask);
        t.start();
        }
    }
